package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 * @ClassName ImageView
 * @Description
 * @Author wangss
 * @date 2020.06.02 12:58
 * @Version 1.0
 */
public class ImageView2Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        Image image = new Image("images/12.jpg");

        ImageView imageView = new ImageView(image);
        imageView.setPreserveRatio(true);
        imageView.setSmooth(true);
        imageView.setFitWidth(500d);
        imageView.setFitHeight(500d);

        System.out.println(imageView.getFitWidth());
        System.out.println(imageView.getFitHeight());
        System.out.println(imageView.prefHeight(-1));

        // 改变图片的圆角
        Rectangle rec = new Rectangle(imageView.getFitWidth(), imageView.prefHeight(-1));
        rec.setArcWidth(50d);
        rec.setArcHeight(50d);
        imageView.setClip(rec);

        // 设置显示图片的哪个位置
        imageView.setX(400d);
        imageView.setY(400d);

        an.getChildren().add(imageView);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ImageViewDemo");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}
